package com.cdtu.backend.utils;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Mika
 * @date 2023/08/20 23:38
 * MediaUtil 多媒体工具类
 **/
@Slf4j
public class MediaUtil {
    // 播放音乐
    public static void playMusic(String songName) {
        // 若启动成功，则开一个线程播放背景音乐：Shed a Light
        Runnable playMusic = () -> {
            InputStream in = ClassLoader.getSystemResourceAsStream("music/" + songName);
            try {
                if (in != null) {
                    Player player = new Player(in);
                    log.info("开始播放音乐：" + songName);
                    player.play();
                }
            } catch (JavaLayerException e) {
                throw new RuntimeException(e);
            }
        };
        new Thread(playMusic).start();
    }
    // 播放视频
    public static void directPlayVideo(String videoName) {
        String workDirectory = System.getProperty("user.dir");
        /** TODO:
         *    1.打包时，需要修改 path，将 dev 替换为 prod
         **/
        String dev = "\\exe\\video\\";
        String prod = "\\video\\";
        String path = workDirectory + prod + videoName;
        log.info("当前工作目录是:" + path);
        try {
            Runtime
                    .getRuntime()
                    .exec("\"C:/Program Files/Windows Media Player/wmplayer.exe\" /fullScreen" + path);
            log.info("正在播放视频：" + path);
        } catch (IOException e) {
            System.out.println();
            log.error("播放错误：视频路径错误，无法正常播放视频");
            System.out.println();
            throw new RuntimeException(e);
        }
    }
}
